package framework;

import interfaces.Board;
import interfaces.Cell;

import java.util.Stack;


/**
 * The Tracker class is used to record past Boards and visual states of
 * respective Boards in order to rewind.
 * Note: with each 'tick', stack will be pushed with new Board
 * @author bcharna
 *
 */
public class Tracker {
	
	Stack<Board> stack = new Stack<Board>();
	
	/**
	 * push a Board onto stack
	 * @param b Board object
	 */
	void pushBoard(BoardImpl b)
	{
		int width = b.getWidth();
		int height = b.getHeight();
		Cell[][] cells = b.getCells();
		Cell[][] ncells = new Cell[height][width];
		for(int i = 0; i < height; i++)
			for(int j = 0; j < width; j++)
				ncells[i][j] = cells[i][j].clone();
		try {
			BoardImpl newb = new BoardImpl(ncells, height, width);
			stack.push(newb);
		} catch (InvalidCellsArrayException e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * pop the last Board from stack
	 * @return last Board object or null if empty
	 */
	Board popBoard()
	{
		if (stack.isEmpty())
			return null;
		return stack.pop();
	}
	
	/**
	 * see if stack is empty
	 * @return true: the stack is empty
	 */
	boolean hasNext()
	{
		return !stack.isEmpty();
	}
	
}
